// 权限
import Vue from 'vue'
import store from './store'
import {getToken} from '@/utils/auth'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import { router, menuRouter, createRouter } from '@/router'

const whileList = ['/login']
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    const token = getToken()
    if(token){
      if(to.path === '/login'){
        next('/')
        NProgress.done()
      }
      if(store.getters.routes && store.getters.routes.length){
        next()
      }else{
      const routes = await store.dispatch('GetInfo')
      if(!routes){
        store.commit('resetUser')
        next('/login')
        NProgress.done()
      }else{
        router.addRoutes(routes)      
        
        next({...to,replace:true})
        NProgress.done()
       }
     }
    }else{
      if(whileList.includes(to.path)){
        next()
      }else{
        next('/login')
        NProgress.done()
      } 
    }
})

router.afterEach(()=>{
  NProgress.done()
})
